package com.vsoft.anti_fra;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import com.vsoft.framework.core.BaseException;
import com.vsoft.framework.core.BusiObj;
import com.vsoft.framework.core.IService;
import com.vsoft.framework.core.db.OperAgent;
import com.vsoft.framework.core.db.SQLArgs;
import com.vsoft.framework.core.util.BOHelper;
import com.vsoft.framework.core.util.LogCollect;

public class StaffConf implements IService {
	private static Logger	vLogger		= Logger.getLogger(RuleConf.class);

	private String				vName			= RuleConf.class.getName();

	private Connection		vConn			= null;

	private SQLArgs				vArg			= new SQLArgs();

	private BusiObj				vInBO			= null;

	private StringBuffer	vStrBuf		= new StringBuffer();

	private String				staffNo		= "";

	private String				staffCode	= "";

	private String				staffName	= "";

	private String				gSql			= "";

	public BusiObj execute(BusiObj obj) throws BaseException {
		vInBO = obj;
		vConn = vInBO.getConnection();
		String s = vInBO.getName();
		System.out.println("--------------------------------------------\n\n" + BOHelper.boToXML(vInBO));
		//BusiObj session = vInBO.getBusiObj("SESSION_DATA");
		staffNo = vInBO.getString("SESSION_STAFF_NO", "");
		staffCode = vInBO.getString("SESSION_STAFF_CODE", "");
		staffName = vInBO.getString("SESSION_STAFF_NAME", "");
		if (s.equals("AF_SetStaffList"))
			return SetStaffList();
		else if (s.equals("AF_GetStaffList"))
			return GetStaffList();

		else
			throw new BaseException(vName + "-001", vInBO.getName() + "没有匹配服务");
	}

	public BusiObj GetStaffList() throws BaseException {
		BusiObj rsBo = null;
		try {
			vArg.reset();
			String staff_code = ((String) vInBO.getValue("staff_code", "")).trim();
			String condition = "";
			boolean condi_flag = false;
			if (!(staff_code.equals("") || staffCode.equals("0001")))
				condi_flag = true;

			if (condi_flag) {
				condition = " and t.staff_code=:staff_code";
				vArg.setArg("staff_code", staff_code);
			}
			gSql = " select t.staff_code,t.name staff_name,t.password,t.state,t.state_time,t.staff_no,a.role_code "
					+ "from staff t,staff_roles a where t.staff_code=a.staff_code" + condition;
			vLogger.info("sql----------:" + gSql);
			rsBo = OperAgent.execQuery(vConn, "GetStaffList", gSql, vArg);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BaseException(vName + "-010", "查询员工信息失败");
		}
		vLogger.info("返回数据是:\n\n\n\n" + BOHelper.boToXML(rsBo));
		vInBO.setValue("ResultFlag", "0");
		return rsBo;
	}

	public BusiObj SetStaffList() throws BaseException {

		//String strType = vInBO.getString("NUM");
		ArrayList<BusiObj> member = vInBO.getBusiObjList("Staff_List");
		String newFlag = vInBO.getString("new_flag"); //0:不重新插入记录。//1：重新插入记录
		for (BusiObj m : member) {

			String staff_code = (String) m.getValue("staff_code", true);

			OperAgent oa = new OperAgent(vConn);
			oa.addSQL("delete staff where staff_code=:staff_code");
			oa.prepare();
			oa.bind("staff_code", staff_code);
			oa.run();
			oa.reset();

			oa.addSQL("delete staff_roles where staff_code=:staff_code");
			oa.prepare();
			oa.bind("staff_code", staff_code);
			oa.run();
			oa.reset();

			if (newFlag.equals("1")) {
				String staff_name = m.getString("staff_name");
				String state = m.getString("state");
				String password = m.getString("password");
				String staff_no = m.getString("staff_no");
				String role_code = m.getString("role_code");
				oa.addSQL("insert into staff t (t.staff_code,t.name,t.password,t.state,t.state_time,t.staff_no ) "
						+ " values (:staff_code,:staff_name,:password,:state,sysdate,:staff_no)");
				oa.prepare();
				oa.bind("staff_code", staff_code);
				oa.bind("state", state);
				oa.bind("staff_name", staff_name);
				oa.bind("password", password);
				oa.bind("staff_no", staff_no);
				oa.run();
				oa.reset();

				oa.addSQL("insert into staff_roles t (t.staff_code,t.role_code ) values (:staff_code,:role_code)");
				oa.prepare();
				oa.bind("staff_code", staff_code);
				oa.bind("role_code", role_code);
				oa.run();
				oa.reset();
			}
		}

		vInBO.setValue("ResultFlag", "0");
		return vInBO;
	}

	public String formatTime(String time, int format_type) throws BaseException {
		String return_value = "";
		int length = time.trim().length();
		if (length == 8)
			return_value = time + " 00:00:01";
		else if (length == 0)
			if (format_type == 1)//开始时间
				return_value = "2000-01-01 00:00:01";
			else
				return_value = "2099-01-01 00:00:01";//结束时间
		else
			return_value = time;
		return return_value;

	}
}
